package com.prime.tutorial.moviestore.view.customer;

import com.prime.tutorial.moviestore.Constants;
import com.prime.tutorial.moviestore.domain.Customers;
import com.prime.tutorial.moviestore.domain.PayCard;
import com.prime.tutorial.moviestore.domain.Payment;
import com.prime.tutorial.moviestore.service.customer.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: vasylja
 * Date: May 8, 2010
 * Time: 7:51:46 PM
 *
 * @author vasylja
 */

@Component("makePayment")
@Scope("request")
public class MakePayment implements Serializable {

    private CustomerService customerService;
    private PayCard payCard;
    private Payment payment;
    private Customers customer;
    private Long code;

    public MakePayment(){

    }

    @Autowired
    public MakePayment(CustomerService customerService) {
        this.setCustomerService(customerService);
    }

    public String makePayment(ActionEvent actionEvent){
//        payCard
        if(getCustomerService().checkCardExists(getCode())){
//        if(payCard!=null){
            payment=new Payment();
            getPayment().setPaymentDate(new Date());
            customer=new Customers();
            FacesContext context = FacesContext.getCurrentInstance();
            setCustomer((Customers)(context.getExternalContext().getSessionMap().get(Constants.CUSTOMER_SESSION_KEY)));
            getPayment().setCustomer(getCustomer());
            getPayment().setPayCard(getCustomerService().getCardWithCode(code).get(0));
            getPayment().setAmount(getPayment().getPayCard().getAmount());
            getPayment().getCustomer().setBalance(getPayment().getCustomer().getBalance()+getPayment().getAmount());
            getCustomerService().makePayment(getPayment());
// Message here.
            String text=Integer.toString(getPayment().getAmount());
//            FacesContext context=FacesContext.getCurrentInstance();
            context.addMessage(null,new FacesMessage(text+" grn added to your account."));
        } else{
//Message here.
            FacesContext context=FacesContext.getCurrentInstance();
            context.addMessage(null,new FacesMessage("Code invalid"));
        }
        return null;
    }

    public CustomerService getCustomerService() {
        return customerService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public PayCard getPayCard() {
        return payCard;
    }

    public void setPayCard(PayCard payCard) {
        this.payCard = payCard;
    }

    public Payment getPayment() {
        return payment;
    }

    public void setPayment(Payment payment) {
        this.payment = payment;
    }

    public Customers getCustomer() {
        return customer;
    }

    public void setCustomer(Customers customer) {
        this.customer = customer;
    }

    public Long getCode() {
        return code;
    }

    public void setCode(Long code) {
        this.code = code;
    }
}
